﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TianYi.Helper
{
  public  class JsonHelper
    {
        /// <summary>
        /// 将泛型对象转换为实体对象
        /// </summary>
        /// <typeparam name="T">实体对象类</typeparam>
        /// <param name="asObject">object对象</param>
        /// <returns></returns>
        public T ConvertObject<T>(object asObject) where T : new()
        {
            //此方法将object对象转换为json字符
            var json = JsonConvert.SerializeObject(asObject);
            //再将json字符转换为实体对象
            var t = JsonConvert.DeserializeObject<T>(json);
            return t;
        }

        /// <summary>
        /// 读取Json文件
        /// </summary>
        /// <typeparam name="T">实体类对象</typeparam>
        /// <param name="path">文件路径</param>
        /// <returns>实体类对象</returns>
        public T GetJsonDeserialize<T>(string path) where T : new()
        {
            string contents = "";
            try
            {
                contents = File.ReadAllText(path);
                Thread.Sleep(10);
                var res = JsonConvert.DeserializeObject<T>(contents);
                return res;
            }
            catch
            {

            }
            return default(T);
        }

        /// <summary>
        /// 读取Json文件
        /// </summary>
        /// <typeparam name="T">实体类对象</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="fileContent">文件内容</param>
        /// <returns>实体类对象</returns>
        public T GetJsonDeserialize<T>(string path, out string fileContent) where T : new()
        {
            string contents = "";
            try
            {
                contents = File.ReadAllText(path);
                Thread.Sleep(10);
                var res = JsonConvert.DeserializeObject<T>(contents);
                fileContent = contents;
                return res;
            }
            catch
            {

            }
            fileContent = contents;
            return default(T);
        }

        /// <summary>
        /// 读取json文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="fileContent">文件内容</param>
        /// <returns></returns>
        public JObject GetJsonDeserialize(string path, out string fileContent)
        {
            string contents = "";
            JObject jo = new JObject();
            try
            {
                contents = File.ReadAllText(path);
                fileContent = contents;
                Thread.Sleep(10);
                jo = (JObject)JsonConvert.DeserializeObject(fileContent);
                return jo;
            }
            catch
            {

            }
            fileContent = contents;
            return jo;
        }

        /// <summary>
        /// 解析json文本中的数组
        /// </summary>
        /// <param name="content">json内容</param>
        /// <returns></returns>
        public JArray GetJsonArray(string content)
        {
            JArray jArray = new JArray();
            try
            {
                jArray = (JArray)JsonConvert.DeserializeObject(content);
                return jArray;
            }
            catch
            {

            }
            return jArray;
        }

        /// <summary>
        /// 获取json文本中的字符串数组
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="varName">变量名称</param>
        /// <returns>字符串数组</returns>
        public string[] GetJsonStringArray(string filePath, string varName)
        {
            string jsonContent = "";
            JObject jo = GetJsonDeserialize(filePath, out jsonContent);
            JArray jArray = GetJsonArray(jo[varName].ToString());
            string[] items = new string[jArray.Count];
            for (int i = 0; i < jArray.Count; i++)
            {
                items[i] = jArray[i].ToString();
            }
            return items;
        }


        /// <summary>
        /// 写入json文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="value">json值</param>
        public void SetJsonSerialize(string path, object value)
        {
            string contents = JsonConvert.SerializeObject(value);
            using (FileStream fs = new FileStream(path, FileMode.Truncate, FileAccess.ReadWrite))
            {
                byte[] buffer = Encoding.ASCII.GetBytes(contents);
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }

        /// <summary>
        /// 写入Json文件
        /// </summary>
        /// <typeparam name="T">实体类对象</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="jsonObject">实体类对象</param>
        public void SetJsonSerialize<T>(string path, T jsonObject) where T : new()
        {
            string contents = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                byte[] buffer = Encoding.ASCII.GetBytes(contents);
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
    }
}
